package com.wiscamp.ninechapters.problems.math;

import com.wiscamp.ninechapters.common.core.ParseHelper;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * 时间
 * 支持小时:分钟 或 小时:分钟:秒 两种格式
 */
@Data
public class LatexTime implements Serializable {
    private int hour = 0;

    private int minute = 0;

    private int second = 0;

    private String timePattern = "HH:mm:ss";

    private LocalTime time;


    public String getLatex() {
        return this.time.format(DateTimeFormatter.ofPattern(this.timePattern));
    }

    public LatexTime(int hour, int minute, int second) {
        this.time = ParseHelper.timeOf(hour, minute, second);

        if (Objects.nonNull(this.time)) {
            this.hour = hour;
            this.minute = minute;
            this.second = second;
        }
    }

    public LatexTime(int hour, int minute) {
        this(hour, minute, 0);
        this.timePattern = "HH:mm";
        this.time = LocalTime.of(this.hour, this.minute, 0);
    }

    public LatexTime(String content) {
        var array = content.split(":");
        if (array.length == 2) {
            this.hour = ParseHelper.parseInteger(array[0]);
            this.minute = ParseHelper.parseInteger(array[1]);
            this.second = 0;
            this.timePattern = "HH:mm";
            this.time = LocalTime.of(this.hour, this.minute, this.second);
        } else if (array.length == 3) {
            this.hour = ParseHelper.parseInteger(array[0]);
            this.minute = ParseHelper.parseInteger(array[1]);
            this.second = ParseHelper.parseInteger(array[2]);
            this.time = LocalTime.of(this.hour, this.minute, this.second);
        }

    }
}
